# 设置基础镜像alpine
FROM alpine:latest
# 设置创建镜像的作者信息
MAINTAINER JPhoebe
# 镜像填写元数据metadata信息
LABEL version="1.0"
LABEL description="Dockerfile TeamCity"
# 更新Alpine的软件源为国内（清华大学）的站点，因为从默认官源拉取实在太慢了。。。
RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/latest-stable/main/" > /etc/apk/repositories
# 更新源
RUN apk update && apk upgrade
# 更新apk-tools
RUN apk add --no-cache apk-tools
# 安装bash
RUN apk add --no-cache bash \
        bash-doc \
        bash-completion \
        && rm -rf /var/cache/apk/* \
        && /bin/bash

# Alpine安装 Glibc https://github.com/sgerrand/alpine-pkg-glibc（jdk依赖）
RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://raw.githubusercontent.com/sgerrand/alpine-pkg-glibc/master/sgerrand.rsa.pub && \
    wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.27-r0/glibc-2.27-r0.apk && \
    apk add glibc-2.27-r0.apk \
    && rm -rf glibc-2.27-r0.apk

# 下载jdk && 安装Jdk 下载链接为一次性，需要从官网重新获取
RUN cd /usr/local/
# RUN wget -q --header "Cookie: oraclelicense=accept-securebackup-cookie" -P /usr/local/ http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jre-8u171-linux-x64.tar.gz
RUN wget -O /usr/local/jdk-8u181-linux-x64.tar.gz http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz?AuthParam=1534169511_ce58077ca3e0f603e3a801738e07e305
RUN tar -zxvf /usr/local/jdk-8u181-linux-x64.tar.gz -C /usr/local/ && rm -rf /usr/local/jdk-8u181-linux-x64.tar.gz
# 下载maven
RUN cd /usr/local/
RUN wget -O /usr/local/apache-maven-3.5.4-bin.tar.gz http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
RUN tar -zxvf /usr/local/apache-maven-3.5.4-bin.tar.gz -C /usr/local/ && rm -rf /usr/local/apache-maven-3.5.4-bin.tar.gz
## 配置jdk/maven环境变量
ENV JAVA_HOME=/usr/local/jdk1.8.0_181
ENV M2_HOME=/usr/local/apache-maven-3.5.4
ENV CLASSPATH=$JAVA_HOME/bin
ENV PATH=.:$JAVA_HOME/bin:$M2_HOME/bin:$PATH

# 下载TeamCity && 安装TeamCity
RUN cd /usr/local/
RUN wget -P /usr/local/ https://download.jetbrains.8686c.com/teamcity/TeamCity-2018.1.tar.gz
RUN tar -zxvf /usr/local/TeamCity-2018.1.tar.gz -C /usr/local/ && rm -rf /usr/local/TeamCity-2018.1.tar.gz

# 安装旧版 npm & nodejs
RUN apk add --no-cache npm
# 安装新版 npm & nodejs
#RUN apk add --no-cache xz
#RUN cd /usr/local/
#RUN wget -O /usr/local/nodejs/node-v10.8.0-linux-x64.tar.xz https://npm.taobao.org/mirrors/node/v10.8.0/node-v10.8.0-linux-x64.tar.xz
#RUN xz -d /usr/local/nodejs/node-v10.8.0-linux-x64.tar.xz
#RUN tar -xvf /usr/local/nodejs/node-v10.8.0-linux-x64.tar -C /usr/local/nodejs && rm -rf /usr/local/nodejs/node-v10.8.0-linux-x64.tar
#RUN ln -s /usr/local/nodejs/node-v10.8.0-linux-x64/bin/node /usr/bin/node
#RUN ln -s /usr/local/nodejs/node-v10.8.0-linux-x64/bin/npm /usr/bin/npm

# 下载&安装破解文件
#RUN wget -P /usr/local/TeamCity/bin http://idea.lanyus.com/jar/JetbrainsCrack-2.9-release-enc.jar

# 暴露TeamCity的端口
EXPOSE 8111

# 新建容器启动脚本
# 安装vim
#RUN apk add --no-cache vim
# 安装sudo
RUN apk add --no-cache sudo
# 创建脚本并授予执行权限
RUN touch /home/start.sh && chmod u+x /home/start.sh
# 在文本中写入脚本命令
RUN echo "cd /usr/local/TeamCity/bin/ && sudo ./teamcity-server.sh start" > /home/start.sh
# 自定义url
#RUN echo "cd /usr/local/TeamCity/buildAgent/bin/ && sudo ./install.sh http://localhost:8111" >> /home/start.sh
# 使用默认配置
RUN echo "cd /usr/local/TeamCity/buildAgent/bin/ && sudo ./agent.sh start" >> /home/start.sh

# 启动执行命令，仅能有一条
# tail -f /dev/null 防止shell执行完后容器就退出的问题
CMD /home/start.sh && tail -f /dev/null

# docker通过Dockerfile创建并启动镜像命令
# docker build -t <标签名> <Dockerfile所在的目录地址>
# docker run -itd -p <物理机端口>:<容器端口> --name <别名> --link <别名:标签> <标签名/镜像id>

# 进入容器( 需要安装bash，并且需要启动容器 )
# docker exec -it <容器id> /bin/bash


# TeamCity Demo
# package
# upload
## %teamcity.build.workingDir%/target/csh.war 打包完成后的文件地址
# restart
## /opt/j2ee/tomcat7_01/bin/shutdown.sh
## rm -rf /home/51jmm/www/csh
## export JAVA_HOME=/usr/local/java/jdk1.8.0_144
## unzip -oq /home/51jmm/www/csh.war -d /home/51jmm/www/csh
## rm /home/51jmm/www/csh.war
## /opt/j2ee/tomcat7_01/bin/shutdown.sh
## export JAVA_HOME=/usr/local/java/jdk1.8.0_144
## sleep 3;/opt/j2ee/tomcat7_01/bin/startup.sh